package code.structure.module.signIn.view
{
	import code.structure.manager.loader.DllLoaderManager;
	import code.structure.manager.pop.view.Windows;
	import code.structure.module.signIn.SignInController;
	import code.structure.module.signIn.SignInModel;
	import code.tools.commponents.CloseBtn;
	import code.tools.commponents.PageTurning;
	import code.untils.SWFNameConst;
	import code.untils.SystemFunction;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.system.System;
	
	public class SignInView extends Windows
	{
		private static const MAX:int = 42;
		private static const ROW:int = 7;
		private var _signInDays:Vector.<SignInDay>;
		/** 补签按钮  */
		private var _retroactiveBtn:SimpleButton;
		/** 签到按钮 */
		private var _signInBtn:SimpleButton;
		private var _controller:SignInController;
		private var _pageTurning:PageTurning;
		public function SignInView(controller:SignInController)
		{
			_controller = controller;
			super();
		}
		
		override protected function initializePanel():void
		{
			super.initializePanel();
			
			var view:Sprite = new (DllLoaderManager.getInstance().getUIMaterial(SWFNameConst.SIGN_IN_PANEL,'view')as Class)as Sprite;
			_mMain.addChild(view);
			
			var closeBtn:SimpleButton = view.getChildByName('closeBtn')as SimpleButton;
			closeBtn.addEventListener(MouseEvent.CLICK,closeHandler);
			
			_retroactiveBtn = view.getChildByName('retroactiveBtn')as SimpleButton;
			_retroactiveBtn.addEventListener(MouseEvent.CLICK,retroactiveBtnHandler);
			
			_signInBtn = view.getChildByName('signInBtn')as SimpleButton;
			_signInBtn.addEventListener(MouseEvent.CLICK,signInBtnHandler);
			
			_signInDays = new Vector.<SignInDay>();
			var i:int,j:int;
			const xpos:Number = 13.7;
			const ypos:Number = 92.6;
			var _x:int = xpos;
			var _y:int = ypos;
			for(; i < MAX; i++,j++)
			{
				if(j != 0)
				{
					if (j % ROW == 0)
					{
						_x=xpos;
						_y+=(SignInDay.HEIGHT + 6.5);
					}
					else
					{
						const temp:int = SignInDay.WIDTH + 3.75;
						_x+=temp;
					}
				}
				var signInDay:SignInDay = new SignInDay(view,_x,_y);
				_signInDays.push(signInDay);
			}
			
			_pageTurning = new PageTurning(view,-10,250,580,pageHandler);
			
			appearFactory(1,1);
		}
		
		override public function get height():Number
		{
			return 498;
		}
		
		override public function get width():Number
		{
			return 752;
		}
		
		private function closeHandler(e:MouseEvent):void
		{
			disappear();
		}
		
		/** 初次签到的日期  */
		private var _firstSignInDate:Date;
		/** 当前日期 */
		private var _currentDate:Date;
		private var _todayDay:int;
		/** 当天是否签到 */
		private var _todaySignIn:Boolean;
		/** 没签到的日期  */
		private var _noSigns:Array;
		private var _signedList:Array;
		public function setData(startTime:int,signedList:Array):void
		{
			_signedList = signedList;
			_signedList.sort();
			/* 初次签到的日期 */
			_firstSignInDate = new Date(startTime * 1000);
			/* 当前日期  */
			_currentDate = new Date(SystemFunction.trueServerTime * 1000);
			/* 30天周期 */
			var cycleTime:int = startTime + (SignInModel.CYCLE_TIME * 86400);
			if(SystemFunction.serverTime > cycleTime)//30天周期已过
			{
				_signedList = [];
				/* 最后一次签到的日期 */
				var lastSignInDate:Date = new Date(_firstSignInDate.fullYear,_firstSignInDate.month,_signedList[_signedList.length - 1]);
				var deltaDay:int = SystemFunction.countDeltaDay(lastSignInDate.time) + 1;//相隔天数
				if(deltaDay >= 5) //超过5天没登录，则记做新的签到循环第一天
					startTime = SystemFunction.serverTime;
				else//超过30天循环，但没有超过5天，则记做新的签到循环第deltaDay天
					startTime = lastSignInDate.time / 1000;
				
				_firstSignInDate = new Date(startTime * 1000);
				cycleTime = startTime + (SignInModel.CYCLE_TIME * 86400);//30天周期 
			}
			_todayDay = _currentDate.date;//今天几号
			
			if(_firstSignInDate.month == _currentDate.month)
				_pageTurning.max = 0;
			else
				_pageTurning.max = 1;
			_pageTurning.value = 1;
			refresh(_currentDate);
		}
		
		private function refresh(d:Date):void
		{
			_noSigns = [];
			var dd:Date = new Date(d.fullYear,d.month,1);
			var upTotalDayNum:int = getDays(dd.fullYear,dd.month);
			var totalDayNum:int = getDays(dd.fullYear,dd.month + 1);
			var nextTotalDayNum:int = getDays(dd.fullYear,dd.month + 2);
			var firstDay:int = dd.day;
			var i:int,j:int;
			/* 取上一个月的补满 */
			j = upTotalDayNum - firstDay + 1;
			for(; i < firstDay; i++,j++)
				_signInDays[i].setData(-1);
			j = 0;
			if(dd.month == _currentDate.month)
			{
				for(; j < totalDayNum; i++,j++)
				{
					if(j < _todayDay)
					{
						if(_signedList.indexOf(j+1) == -1)
						{
							if((j + 1) == _todayDay)
							{
								if(!_todaySignIn)
									_todaySignIn = true;
								_signInDays[i].setData(j + 1, SignInDay.STATE_CAN_SIGN_IN);
							}
							else
							{
								_signInDays[i].setData(j + 1, SignInDay.STATE_NO_SIGN_IN);
								if(j + 1 < d.date)
									_noSigns.push(j + 1);
							}
						}
						else
							_signInDays[i].setData(j + 1,SignInDay.STATE_HAVE_SIGN_IN);
					}
					else
						_signInDays[i].setData(j + 1,SignInDay.STATE_NO_CAN_SIGN_IN);
				}
			}
			else
			{
				for(; j < totalDayNum; i++,j++)
				{
					if(_signedList.indexOf(j+1) != -1 && (j + 1) >= d.date)//在已签到的列表里，并且大于首次签到的日期
						_signInDays[i].setData(j + 1,SignInDay.STATE_HAVE_SIGN_IN);
					else{
						_signInDays[i].setData(j + 1, SignInDay.STATE_NO_SIGN_IN);
						if(j + 1 > d.date  && j +1 < d.dateUTC)
							_noSigns.push(j + 1);
					}
				}
			}
			/* 取下一个月的补满 */
			j = 0;
			for(; j < nextTotalDayNum; i++,j++)
			{
				if(i == _signInDays.length)break;
				_signInDays[i].setData(-1);
			}
			
			_retroactiveBtn.enabled = _noSigns.length != 0;
			_signInBtn.enabled = !_todaySignIn;
		}
		
		public function set totalRewardList(d:Array):void
		{
		}
		
		private function retroactiveBtnHandler(e:MouseEvent):void
		{
			_controller.signInOffsetHandler(_noSigns[0]);
		}
		
		private function signInBtnHandler(e:MouseEvent):void
		{
			_controller.signInHandler(_todayDay);
		}
		
		private function getDays(year:int,month:int):int
		{
			if(month == 2)
			{
				if((year % 400 == 0) || ( (year % 100 != 0) && (year % 4 == 0) ))
					return 29;
				return 28;
			}
			else if(month == 4 || month == 6 || month == 9 || month == 11)
				return 30;
			return 31;
		}
		
		private function pageHandler(...args):void
		{
			refresh(_pageTurning.value == 0 ? _firstSignInDate : _currentDate);
		}
	}
}